const { v4: uuidv4 } = require("uuid")

const BaseService = require("./BaseService")
class PersonService extends BaseService {
  constructor() {
    super()
  }
  // 查找
  getList({ page = 1, pageSize = 10, status, id } = {}) {
    // 初始化状态
    status = (status === "0" || status === 0) ? 0 : 1
    let filter = ` WHERE status=${status}`
    if (id) {
      filter = filter + ` AND id='${id}'`
    }
    const { total, rows } = this.getListByPage({ tableName: "person", filter: filter + " ORDER BY name DESC", page, pageSize })
    rows.map((row) => {
      return row
    })
    return { total, rows }
  }
  // 创建对象并保存
  create({ name, password }) {
    const person = {
      id: uuidv4(),
      name,
      password,
      status: 1,
    }

    this.insert("person", person)

    return person
  }
  // 更新姓名
  updateName(id, name) {
    const person = this.getOneById("person", id)
    if (!person) {
      throw new Error(`记录 ${id} 不存在`)
    }
    return this.db.update(`UPDATE person SET name=? WHERE id=?`, name, updateTime, id)
  }
  // 删除
  deleteOneById(id) {
    const person = this.getOneById("person", id)
    if (!person) {
      throw new Error(`记录 ${id} 不存在`)
    }
    return this.db.update(`UPDATE person SET status=0 WHERE id=?`, id)
  }
}
module.exports = PersonService
